package com.ruoyi.manage.service.impl;

import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.DictUtils;
import com.ruoyi.common.utils.PrimaryKeyUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.manage.config.AppConfig;
import com.ruoyi.manage.domain.AppUser;
import com.ruoyi.manage.domain.Jurisdiction;
import com.ruoyi.manage.domain.Order;
import com.ruoyi.manage.domain.TypeTemplate;
import com.ruoyi.manage.domain.request.OrderRequest;
import com.ruoyi.manage.mapper.DeptMapper;
import com.ruoyi.manage.mapper.JurisdictionMapper;
import com.ruoyi.manage.mapper.OrderMapper;
import com.ruoyi.manage.mapper.TypeTemplateMapper;
import com.ruoyi.manage.service.IAppOrderService;
import com.ruoyi.manage.service.IAppUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: wwy
 * @Date: 2021/1/7 8:52
 */
@Service
public class AppOrderServiceImpl implements IAppOrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private DeptMapper deptMapper;
    @Autowired
    private IAppUserService appUserService;
    @Autowired
    private JurisdictionMapper jurisdictionMapper;
    @Override
    public List<Order> getOrder(OrderRequest orderRequest, String openId) {
        if(StringUtils.isEmpty(openId)){
            throw new CustomException("没有权限", HttpStatus.UNAUTHORIZED);
        }
        Order order = new Order();
        order.setOpenId(openId);
        if(orderRequest != null){
            order.setStatus(orderRequest.getStatus());
        }
        List<Order> byOpenId = orderMapper.findByUser(order);
        return byOpenId;
    }

    @Override
    public AjaxResult addOrder(Order order, String openId) {
        //判断用户是否被冻结
        if(getUserStatus(openId) != AppConfig.APP_STATUS_NORMAL){
            throw new CustomException("账号已被冻结", HttpStatus.SUSPEND_ACCOUNT);
        }
        if(StringUtils.isEmpty(openId)){
            throw new CustomException("没有权限", HttpStatus.UNAUTHORIZED);
        }
        if(order == null || StringUtils.isEmpty(order.getDeptId()) || StringUtils.isEmpty(order.getJurisdictionId())){
            throw new CustomException("参数错误", HttpStatus.BAD_REQUEST);
        }
        //判断上传图片个数
        String jurisdictionId = order.getJurisdictionId();
        if(jurisdictionId == null){
            throw new CustomException("参数错误", HttpStatus.BAD_REQUEST);
        }
        //根据辖区id查询出辖区的图片最小限制个数
        Jurisdiction jurisdiction = jurisdictionMapper.selectJurisdictionById(Long.parseLong(jurisdictionId));
        if(jurisdiction == null){
            throw new CustomException("系统内部错误", HttpStatus.ERROR);
        }
        String pictureList = order.getPictureList();
        if(StringUtils.isEmpty(pictureList)){
            pictureList = "";
        }
        String[] imageList = pictureList.split(",");
        Integer uploadImageMin = jurisdiction.getUploadImageMin();
        String video = order.getVideo();

        if(StringUtils.isEmpty(video) && uploadImageMin>imageList.length){
            //图片数量不够
            throw new CustomException("图片至少上传"+uploadImageMin+"张", HttpStatus.ERROR);
        }

        order.setOpenId(openId);
        String deptName = order.getDeptId();
        SysDept sysDept = deptMapper.findByName(deptName);
        if(sysDept == null){
            throw new CustomException("系统错误,无此部门记录", HttpStatus.ERROR);
        }
        order.setDeptId(String.valueOf(sysDept.getDeptId()));
        order.setStatus(0);
        order.setProfessionalId(PrimaryKeyUtil.nextId()+"");
        AppUser appUser = this.getAppUser(openId);
        String phone = appUser.getPhone();
        String identitynumber = appUser.getIdentitynumber();
        String name = appUser.getName();
        order.setIdNumber(identitynumber);
        order.setPhone(phone);
        order.setReportName(name);
        int i = orderMapper.insertOrder(order);
        return AjaxResult.success("添加成功");
    }

    @Override
    public AjaxResult getOrder(OrderRequest orderRequest) {
        if(orderRequest == null || orderRequest.getId() == null){
            throw new CustomException("参数错误", HttpStatus.BAD_REQUEST);
        }
        List<SysDictData> car_type_list = DictUtils.getDictCache("car_type");
        Order order = orderMapper.selectOrderById(orderRequest.getId());
        for (SysDictData sysDictData : car_type_list) {
            String dictLabel = sysDictData.getDictLabel();
            String dictValue = sysDictData.getDictValue();
            if(dictValue.equals(order.getCarType()+"")){
                order.setCarTypeName(dictLabel);
            }
        }

        return AjaxResult.success(order);
    }

    @Override
    public AjaxResult getImageCount(Long jurisdictionId) {
        Jurisdiction jurisdiction = jurisdictionMapper.selectJurisdictionById(jurisdictionId);
        return AjaxResult.success(jurisdiction);
    }

    private int getUserStatus(String openId){
        AppUser appUser = new AppUser();
        appUser.setOpenId(openId);
        List<AppUser> appUsers = appUserService.selectAppUserList(appUser);
        AppUser appUser1 = appUsers.get(0);
        return appUser1.getStatus();
    }

    /**
     * 获取用户信息
     * @param openId
     * @return
     */
    private AppUser getAppUser(String openId){
        AppUser appUser = new AppUser();
        appUser.setOpenId(openId);
        List<AppUser> appUsers = appUserService.selectAppUserList(appUser);
        AppUser appUser1 = appUsers.get(0);
        return appUser1;
    }


}
